package com.gupao.p7store.common.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


/**
 * desc: 可感知的 Spring 上下文
 *
 * @ClassName: com.gupao.p7store.common.aware.AwareSpringContext
 * @Author: dengbicheng
 * @CreateTime: 2022-02-08 16:43:23
 * @Version: v 1.0.0
 */
@Component
public class AwareSpringContext implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    public static <T> T getBean(Class<T> type) {
        if (context == null) {
            return null;
        }
        return context.getBean(type);
    }

    public static <T> T getBean(String name, Class<T> type) {
        if (context == null) {
            return null;
        }
        return context.getBean(name, type);
    }

    public static Object getBean(String name) {
        if (context == null) {
            return null;
        }
        return context.getBean(name);
    }
}
